/**
 * Copyright (c) 2010-2020 by skzr.org
 * All rights reserved.
 * @Author skzr.org(E-mail:skzr.org@gmail.com)
 * @date 2010-11-8 下午11:23:34
 */
package org.skzr.chart4me.demo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.jfree.data.category.DefaultCategoryDataset;

/**
 * 随机选择某个(i,j)移动其value正负5个位置
 * @jdk 1.6
 * @author skzr.org(E-mail:skzr.org@gmail.com)
 * @version 1.0.0
 */
public class Animator extends Timer implements ActionListener {
	private static final long serialVersionUID = 1L;
	private DefaultCategoryDataset dataset;
	
	
	public Animator(DefaultCategoryDataset dataset) {
		super(20, null);
		this.dataset = dataset;
		addActionListener(this);
	}
	
	public Animator(int delay, DefaultCategoryDataset dataset) {
		super(delay <= 0 ? 20 : delay, null);
		this.dataset = dataset;
		addActionListener(this);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		int i = (int) (Math.random() * dataset.getRowCount());
		Comparable<?> rowKey = dataset.getRowKey(i);
		int j = (int) (Math.random() * dataset.getColumnCount());
		Comparable<?> columnKey = dataset.getColumnKey(j);
		int k = Math.random() - 0.5 >= 0 ? 5 : -5;
		dataset.setValue(Math.max(0, dataset.getValue(i, j).doubleValue() + k), rowKey, columnKey);
	}

}
